package com.video.main_video.common.cutException;

import com.video.main_video.common.response.ZouResponseBody;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @auth wdx
 * @date 2022/4/9
 * 自定义异常处理类
 *
 * */
@ControllerAdvice
public class ResponseHandlerException {
    ResponseExceptionRes res=new ResponseExceptionRes();
    @ZouResponseBody
    @ExceptionHandler(value = ResponseException.class)
    public ResponseExceptionRes handle(ResponseException e){
        String msg = e.getErrorMsg()==null?"":e.getErrorMsg();
        res.setError(e.getExceptionEnum().getMsg()+":"+msg);
        res.setCode(e.getExceptionEnum().getCode());
        return res;
    }

    @ZouResponseBody
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResponseExceptionRes validException(MethodArgumentNotValidException e){
        res.setCode(1009);
        res.setError(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
        return res;
    }

    @ZouResponseBody
    @ExceptionHandler(value = NoSuchMethodException.class)
    public ResponseExceptionRes noMethodException(NoSuchMethodException noMethod){

        res.setCode(1009);
        res.setError("bean 没有handler方法");
        return res;
    }

}
